package com.lx.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.lx.entity.LxUser;
import com.lx.service.impl.LxUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.HashMap;

/**
 * @Description:
 * @author: xuaner
 * @date: 2021年09月14日 21:56
 */

@RestController
public class AccountController {
    @Autowired
    LxUserServiceImpl lxUserService;
    @Autowired
    private Producer captchaProducer = null;

    private String titlecode = "null";
    @RequestMapping("/kaptcha")
    @CrossOrigin
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HashMap<Object, Object> resultMap = new HashMap<>();

        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        //生成验证码
        String capText = captchaProducer.createText();
        titlecode = capText;
        System.out.println(capText);
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        //向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

    @CrossOrigin
    @RequestMapping("/checkKapt")
    public String checkKapt(){
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code",titlecode);
        return JSON.toJSONString(resultMap);
    }


    @CrossOrigin
    @RequestMapping("/login")
    public String login(@RequestBody LxUser lxUser, HttpSession httpSession){
        System.out.println(lxUser);

        LxUser lxUser1 = lxUserService.getOne(new QueryWrapper<LxUser>().eq("u_name",lxUser.getUName()));
        if(lxUser1==null){
            System.out.println("用户不存在");
            HashMap<String,Object> resultData = new HashMap<>();
            resultData.put("msg","用户不存在");
            return JSON.toJSONString(resultData);
        }else if(!lxUser.getUPassword().equals(lxUser1.getUPassword())){
            System.out.println("密码不正确");
            HashMap<String,Object> resultData = new HashMap<>();
            resultData.put("msg","密码不正确");
            System.out.println(JSON.toJSONString(resultData));
            return JSON.toJSONString(resultData);
        }
        System.out.println("登陆成功");
        httpSession.setAttribute("lxUser",lxUser1);
        HashMap<String,Object> resultData = new HashMap<>();
        resultData.put("msg","登陆成功");
        resultData.put("uId",lxUser1.getUId());
        resultData.put("uName",lxUser1.getUName());
        resultData.put("uPassword",lxUser1.getUPassword());
        resultData.put("uAuth",lxUser1.getUAuth());
        return JSON.toJSONString(resultData);

    }

}
